<?php

namespace app\common\service;


use think\Db;

/**
 * 常用类
 * jy
 * @package app\common\service
 */
class ToolService
{
    //判断下载或者返回处理
    public static function downOrReturn($db,$param,$titles=null,$function=null){
        if(!empty($param['down'])&&$param['down']==1){
            //如果是下载导出
            return \app\common\service\DownService::getData($db,$titles,$function,$param);
        }else{
            if (empty($param['limit'])) {
                $param['limit'] = 20;
            }
            if (empty($param['page'])) {
                $param['page'] = 1;
            }
            $res=[];
            $res['code']=1;
            //如果$db是sql语句
            if (is_string($db)) {
                $sqlTemp = stristr($db, "limit", true);
                $offset = ($param['page'] - 1) * $param['limit'];
                if ($sqlTemp) {
                    $sql = $sqlTemp . " limit {$offset},{$param['limit']} ";
                }else{
                    $sql=$db . " limit {$offset},{$param['limit']} ";
                }
                $totalSql='SELECT count(*) as total '.stristr($sql,'from');
                $totalTempSql=stristr($totalSql,'limit',true);
                if($totalTempSql){
                    $totalSql=$totalTempSql.' limit 1';
                }else{
                    $totalSql=$totalSql.' limit 1';
                }
                $total=\think\Db::query($totalSql);
                if(!empty($total)){
                    $res['count']=$total[0]['total'];
                }else{
                    $res['count']=0;
                }
                $data=\think\Db::query($sql);
                if(empty($data)){
                    $res['data']=[];
                }else{
                    if(!empty($function)){
                        $res['data']=call_user_func($function,$data,$param);
                    }else{
                        $res['data']=$data;
                    }
                }
            }else{
                if(!isset($param['total'])){
                    $total=clone $db;
                }
                //db对象处理
               $db->page($param['page'], $param['limit']);
               $data=$db->select();
                if(isset($param['total'])){
                    $res['count']=$param['total'];
                }else{
                    $bind=$db->getBind2();
                    $total->setBind($bind);
                    if(!empty($param['_cache'])){
                        $res['count']=$total->cache($param['_cache'],86400)->count();
                    }else{
                        $res['count']=$total->count();
                    }
                }
               if(empty($data)){
                   $res['data']=[];
               }else{
                   if(!empty($function)){
                       $res['data']=call_user_func($function,$data,$param);
                   }else{
                       $res['data']=$data;
                   }
               }
            }
            return $res;
        }
    }

    public static function skuLog($sku,$data){
        $info1=Db::table('skus')->where('sku','in',$sku)->select();
        foreach ($info1 as $v1){

        }

        Db::table('sku_attribute')->where('sku','in',$sku)->select();
    }

}
